import QRCode from 'qrcode';
import html2canvas from 'html2canvas';
export default {
    downloadImg() {
        let name = `${new Date().getTime()}_${Math.random().toString(36).slice(2)}`;
        let type = this.wechatSrc.slice(11, 14)

        const a = document.createElement('a')
        a.download = name + '.' + type;
        a.href = this.wechatSrc
        a.click();
        a.remove()
    },
    getUrl() {
        let {
            article_id,
            details: {
                title,
                imageSrc: [img]
            }
        } = this
        return encodeURIComponent('https://static-d33a5037-6c63-4e92-8bb1-30018ab701ea.bspapp.com/toutiao/index.html#/article?_id=' + article_id)
    },
    getTitle() {
        let {
            details: {
                title,
                imageSrc: [img]
            }
        } = this

        title += "(想看更多消息，欢迎来到莱头条)#头条"
        title = encodeURIComponent(title)
        return title
 
    },
    getPic() {
        let {
            details: {
                title,
                imageSrc: [img]
            }
        } = this
        let _img = 'http://toutiao.longxiaokj.com/1667289499723-th.jfif'
        img = img || _img
        return encodeURIComponent(img)

    },
    shareWX() {
        this.wechatShow = true
        this.test = '保存图片，分享给好友'
        let url = decodeURIComponent(this.getUrl())
        QRCode.toDataURL(url)
            .then(url => {
                console.log(url)
                this.wechatSrc = url
            })
            .catch(err => {
                console.error(err)
            })
    },
    shareSina() {
        let title = this.getTitle()
        let img = this.getPic()
        window.open(`https://service.weibo.com/share/share.php?title=${title}&pic=${img}`)

    },
    shareQQ() {
        let url = this.getUrl()
        let title = this.getTitle()
        let img = this.getPic()
        window.open(`https://connect.qq.com/widget/shareqq/index.html?url=${url}&title=${title}&pics=${img}&site=头条`)
    },
    shareQrcode() {
        this.wechatShow = true
        this.test = '点击下载二维码'
        let url = decodeURIComponent(this.getUrl())
        QRCode.toDataURL(url)
            .then(url => {
                console.log(url)
                this.wechatSrc = url
            })
            .catch(err => {
                console.error(err)
            })

    },
    sharePoster() {
        let poster = document.querySelector("#poster")
        html2canvas(poster, {
            allowTaint: true,
            useCORS: true
        }).then(function (canvas) {
            let ctx=canvas.getContext('2d')
            // ctx.drawImage()
            let img= new Image()
            img.src=require('@/assets/1.png')
            img.onload=function (params) {
                ctx.drawImage(img,0, 0,32,32,0,0,64,48)
                let url = canvas.toDataURL('image/png')
                console.log(url);
            }
           
        });
    },
    shareLink() {
        // 先动态生成一个input
        let input = document.createElement('input')
        // 让input里面的内容为标题和链接
        input.value = ' 复制↓祊蛰塅吪，筏颂给嚎莜！' + this.details.title + '' + this.article_id
        document.body.appendChild(input)
        input.select() //select让他里面的字选中
        document.execCommand('copy')
        input.remove()
        this.$toast('复制成功')

    },
    onSelect({
        name
    }) {
        this.showShare = false;
        let shareMethods = {
            微信: this.shareWX,
            QQ: this.shareQQ,
            微博: this.shareSina,
            二维码: this.shareQrcode,
            分享海报: this.sharePoster,
            复制链接: this.shareLink
        }
        shareMethods[name].call(this)
    },
}